package com.itheima.ydd.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.PageInfo;
import com.itheima.ydd.pojo.YddUser;
import com.itheima.ydd.service.YddUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

//用户管理
@RestController
@RequestMapping("ydd_user")
@Slf4j
public class YddUserController {

    @Autowired
    private YddUserService yddUserService;

    /**
     * 用户添加
     * @param yddUser
     * @return
     */
    @PostMapping("add")
    public ResponseEntity<R<String>> add(@RequestBody YddUser yddUser){
        return yddUserService.addYddUser(yddUser);
    }

    /**
     * 用户删除
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResponseEntity<R<String>> del(@RequestParam Integer id){
        return yddUserService.delYddUser(id);
    }

    /**
     * 修改
     */
    @PostMapping("update")
    public ResponseEntity<R<String>> update(@RequestBody YddUser yddUser) {
        return yddUserService.updateYddUser(yddUser);
    }

    /**
     * 分页
     * @param page
     * @param limit
     * @param sex
     * @param username
     * @param mobile
     * @return
     */
    @GetMapping("list")
    public ResponseEntity <R<PageInfo<YddUser>>> list (@RequestParam(value="page",required=false,defaultValue = "1") Integer page,
                                                       @RequestParam(value="limit",required=false,defaultValue = "10") Integer limit,
                                                       @RequestParam(value="sex",required=false) String sex,
                                                       @RequestParam(value="username",required=false) String username,
                                                       @RequestParam(value="mobile",required=false) String mobile) {

        return yddUserService.findPage(page,limit,sex,username,mobile);
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResponseEntity<R<YddUser>> rowInfo(@RequestParam(value = "id") String id){
        YddUser yddUser = yddUserService.getById(id);
        log.info(yddUser.toString());
        return ResponseEntity.ok(R.success(yddUser));
    }
}
